package cn.com.sports.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.sports.model.User;
import cn.com.sports.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService ;
	@RequestMapping("/addUserUI")
	public String addUI(){
		return "/user/addUser";
	}
	@RequestMapping("/addUser")
	public String addUser(User user,ModelMap modelMap){
		userService.saveUser(user);
		List<User> users = userService.getAllUser();
		modelMap.addAttribute("users",users);
		return "redirect:/user/showUsers.html";
	}
	@RequestMapping("/showUsers")
	public String showUsers(ModelMap modelMap){
		List<User> users = userService.getAllUser();
		modelMap.addAttribute("users",users);
		return "/user/showUsers";
	}
	@RequestMapping("/delUser/{userId}")
	public ModelAndView showUsers(ModelMap modelMap,@PathVariable int userId){
		userService.delUserById(userId);
		return new ModelAndView("redirect:/user/showUsers.html");
	}
	@RequestMapping("/editUserUI/{userId}")
	public String editUserUI(ModelMap modelMap,@PathVariable int userId){
		User user = userService.getUserById(userId);
		modelMap.addAttribute("user",user);
		return "/user/editUser";
	}
	@RequestMapping("/editUser")
	public String editUserUI(User user,ModelMap modelMap){
		int flag =  userService.editUser(user);
		modelMap.addAttribute("flag",flag);
		return "/user/editUser";
	}
}
